/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package wargame.dao;

import wargame.entity.*;
import connectionmanager.DB;

/**
 *
 * @author Clarence
 */
public class EconomicDAO {

    public static Economic retrieveEconomic(double economic_id) {
        String sql = "SELECT `economic_id`, `turn`, `economic_index` FROM `economic` WHERE economic_id = '" + economic_id + "'";
        DB db = new DB();
        db.connect();
        db.query(sql);
        if (db.next()) {
            Economic e = new Economic();
            e.setEconomic_id(db.getInt("economic_id"));
            e.setTurn(db.getInt("turn"));
            e.setEconomic_index(db.getDouble("economic_index"));
            return e;

        } else {
            return null;
        }
    }

    public static Economic retrieveEconomic(int turn) {
        String sql = "SELECT `economic_id`, `turn`, `economic_index` FROM `economic` WHERE turn = '" + turn + "'";
        DB db = new DB();
        db.connect();
        db.query(sql);
        if (db.next()) {
            Economic e = new Economic();
            e.setEconomic_id(db.getInt("economic_id"));
            e.setTurn(db.getInt("turn"));
            e.setEconomic_index(db.getDouble("economic_index"));
            return e;

        } else {
            return null;
        }
    }

    public static double insertEconomic(Economic e) {
        String sql = "INSERT INTO economic (`turn`, `economic_index`) VALUES ('" + e.getTurn() + "', '" + e.getEconomic_index() + "');";
        DB db = new DB();
        db.connect();
        db.execute(sql);
        int lastInsertedId = db.getLastInsertedID();
        db.close();
        return lastInsertedId;
    }

    public static void updateEconomic(Economic e) {
        String sql = "UPDATE economic SET `turn` = '" + e.getTurn() + "', `economic_index` = '" + e.getEconomic_index() + "' WHERE economic_id = '" + e.getEconomic_id() + "';";
        DB db = new DB();
        db.connect();
        db.execute(sql);
        db.close();
    }

    public static void clear(){
         String sql = "TRUNCATE TABLE economic";
        DB db = new DB();
        db.connect();
        db.execute(sql);
        db.close();
    }
}
